package Bean;

import java.lang.reflect.Field;

/**
 * @Author czl
 * @Date 2024-03-27
 */
public class BeanUtil {

    private String name;
    private int age;
    private Double weight;
    private static final double PI = 3.14;

    public BeanUtil() {
    }

    public BeanUtil(String name, int age, Double weight) {
        this.name = name;
        this.age = age;
        this.weight = weight;
    }

    public BeanUtil(String name, Double weight) {
        this.name = name;
        this.age = 18;
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Double getWeight() {
        return weight;
    }

    public void setWeight(Double weight) {
        this.weight = weight;
    }

    public void hello() {
        System.out.println("你好!");
    }

    private void sleep() {
        System.out.println("睡觉了！");
    }

    public void sayHello(String who) {
        System.out.println("你好," + who);
    }

    public int sum(int i, int j) {
        return i + j;
    }

    @Override
    public String toString() {
        return this.getName()+","+this.getAge()+","+this.getWeight() ;
    }

    public void show() {
        System.out.println("姓名：" + this.name);
        System.out.println("年龄：" + this.age);
        System.out.println("体重：" + this.weight);
    }

    public void Students(String name,Double score){
        this.name=name;
        this.weight=score;
    }

    public static void setProperty(Object bean, String propertyName, Object value) throws Exception{

        Class<?> clazz = bean.getClass();

        Field declaredField = clazz.getDeclaredField(propertyName);

        declaredField.setAccessible(true);

        declaredField.set(bean,value);
    }
    public static Object getProperty(Object bean, String propertyName) throws Exception{

        Class<?> clazz = bean.getClass();

        Field declaredField = clazz.getDeclaredField(propertyName);

        declaredField.setAccessible(true);

        return declaredField.get(bean);
    }


    public static void copyProperties(Object source, Object target) throws Exception{

        Class<?> clazz = source.getClass();

        Field[] declaredFields = clazz.getDeclaredFields();

        for (Field declaredField : declaredFields) {

            declaredField.setAccessible(true);

            String name = declaredField.getName();

            Object obj = declaredField.get(source);

            setProperty(target,name,obj);

        }
    }
}
